package top.tervest.easyshare.common.security.handler;

import com.alibaba.fastjson.JSON;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;
import top.tervest.easyshare.common.Result;
import top.tervest.easyshare.common.constant.ResponseState;
import top.tervest.easyshare.common.util.HttpUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ZhangNanNan
 * @version v
 * @create 2019-05-17 10:53
 */
@Component("customAccessDeniedHandlerImpl")
public class CustomAccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        //权限不足
        Result failure = Result.failure(ResponseState.ACCESS_DENIED, accessDeniedException.getMessage());
        HttpUtil.writeJson(response,failure);
    }
}
